package com.dengcj.epl.service.ecology.workflow;

import cn.hutool.json.JSONObject;
import com.dengcj.epl.entity.ApiResponseResult;
import com.dengcj.epl.service.util.EcologyApiUtil;
import com.dengcj.epl.service.util.WorkflowUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * @author dengchunjie
 * @Description: 流程干预工具 用于流程提交、退回、干预、强制归档
 * @date 2021/12/17 19:37
 */
public class RequestIntervene {

    private EcologyApiUtil ecologyApiUtil = new EcologyApiUtil();

    /**
     * 提交到下一节点
     * @param requestId requestid
     * @param operUser 节点操作者
     * @param requestLog 签字意见
     * @return
     */
    public ApiResponseResult submit(int requestId, int operUser, String requestLog) {
        Map<String, Object> parmaMap = new HashMap<>();
        parmaMap.put("requestId", requestId);
        parmaMap.put("remark", requestLog);
        return ecologyApiUtil.commonRequest(operUser, "/api/workflow/paService/submitRequest", parmaMap);
    }

    /**
     * 按出口退回至上一节点
     * @param requestId requestid
     * @param operUser 节点操作者
     * @return
     */
    public ApiResponseResult reject(int requestId, int operUser) {
        Map<String, Object> parmaMap = new HashMap<>();
        parmaMap.put("requestId", requestId);
        return ecologyApiUtil.commonRequest(operUser, "/api/workflow/paService/rejectRequest", parmaMap);
    }

    /**
     * 退回至指定节点
     * @param requestId requestid
     * @param operUser 节点操作者
     * @param nodeid 节点ID
     * @return
     */
    public ApiResponseResult reject2Node(int requestId, int operUser, int nodeid) {
        JSONObject otherParams = new JSONObject();
        otherParams.set("RejectToType", 0);
        otherParams.set("RejectToNodeid", nodeid);

        Map<String, Object> parmaMap = new HashMap<>();
        parmaMap.put("requestId", requestId);
        parmaMap.put("otherParams",otherParams.toString());
        return ecologyApiUtil.commonRequest(operUser, "/api/workflow/paService/rejectRequest", parmaMap);
    }

    /**
     * 退回至指定节点
     * @param requestId requestid
     * @param operUser 节点操作者
     * @param nodeName 节点名称
     * @return
     */
    public ApiResponseResult reject2Node(int requestId, int operUser, String nodeName) {
        int nodeId = WorkflowUtil.getNodeIdByNameAndRequestId(requestId, nodeName);
        return reject2Node(requestId, operUser, nodeId);
    }

    /**
     * 退回至发起节点
     * @param requestId requestid
     * @param operUser 节点操作者
     * @return
     */
    public ApiResponseResult reject2Creator(int requestId, int operUser) {
        int startNodeId = WorkflowUtil.getStartNodeByRequestId(requestId);
        return reject2Node(requestId, operUser, startNodeId);
    }

    /**
     * 流程干预
     * @param requestId requestid
     * @param targetNode 目标节点
     * @param requestLog 签字意见
     * @param executeNodeBefore 是否执行节点前附加操作
     * @return
     */
    public ApiResponseResult intervene(int requestId, int targetNode, String requestLog, boolean executeNodeBefore) {
        Map<String, Object> parmaMap = new HashMap<>();
        parmaMap.put("requestId", requestId);
        parmaMap.put("submitNodeId", targetNode);
        parmaMap.put("remark", requestLog);
        parmaMap.put("enableIntervenor", executeNodeBefore);
        return ecologyApiUtil.commonRequest(1, "/api/workflow/paService/doIntervenor", parmaMap);
    }

    /**
     * 流程干预
     * @param requestId requestid
     * @param targetNode 目标节点
     * @param requestLog 签字意见
     * @param executeNodeBefore 是否执行节点前附加操作
     * @param receiver 接收人，多个人员以英文逗号隔开
     * @param signType  0 ：非会签 1：会签 2：依次逐个处理
     * @return
     */
    public ApiResponseResult intervene(int requestId, int targetNode, String requestLog, boolean executeNodeBefore, String receiver, int signType) {

        Map<String, Object> parmaMap = new HashMap<>();
        parmaMap.put("requestId", requestId);
        parmaMap.put("submitNodeId", targetNode);
        parmaMap.put("remark", requestLog);
        parmaMap.put("enableIntervenor", executeNodeBefore);
        parmaMap.put("Intervenorid", receiver);
        parmaMap.put("SignType", signType);
        return ecologyApiUtil.commonRequest(1, "/api/workflow/paService/doIntervenor", parmaMap);
    }

    /**
     * 强制归档（默认以系统管理员身份、监控模式）
     * @param requestId
     * @return
     */
    public ApiResponseResult forceOver(int requestId){
        Map<String, Object> parmaMap = new HashMap<>();
        parmaMap.put("requestId", requestId);
        parmaMap.put("otherParams", "{\"ismonitor\":\"1\"}");
        return ecologyApiUtil.commonRequest(1, "/api/workflow/paService/doForceOver", parmaMap);
    }
}
